-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSolution.java
63 lines (48 loc) · 1.56 KB
/
Solution.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import java.util.*;
class SnakeAndLadder {
static class Node {
int vertex, distance;
Node(int vertex, int distance) {
this.vertex = vertex;
this.distance = distance;
}
}
public static int minDiceThrows(int[] board, int N) {
boolean[] visited = new boolean[N];
Queue<Node> queue = new LinkedList<>();
queue.add(new Node(0, 0)); // Start from the first cell
visited[0] = true;
while (!queue.isEmpty()) {
Node current = queue.poll();
int v = current.vertex;
if (v == N - 1) {
return current.distance; // Reached the last cell
}
for (int dice = 1; dice <= 6 && v + dice < N; dice++) {
int next = v + dice;
if (!visited[next]) {
visited[next] = true;
int nextVertex = board[next] != -1 ? board[next] : next;
queue.add(new Node(nextVertex, current.distance + 1));
}
}
}
return -1; // No solution
}
public static void main(String[] args) {
int N = 30;
int[] board = new int[N];
Arrays.fill(board, -1);
// Example: Add ladders
board[2] = 21;
board[4] = 7;
board[10] = 25;
board[19] = 28;
// Example: Add snakes
board[26] = 0;
board[20] = 8;
board[16] = 3;
board[18] = 6;
System.out.println("Minimum dice throws required: " + minDiceThrows(board, N));
}
}